<?php

    /**
     *      [Discuz!] (C)2001-2099 Comsenz Inc.
     *      This is NOT a freeware, use is subject to license terms
     *      $Id: table_mobile_setting.php 31281 2012-08-03 02:29:27Z zhangjie $
     */

    if (!defined('IN_DISCUZ')) {
        exit('Access Denied');
    }

    class table_cloud_role_purview extends discuz_table {

        public function __construct() {
            $this->_table = 'cloud_role_purview';
            $this->_pk = 'id';

            parent::__construct();
        }

        //插入数据库
        public function insert($arr) {
            return DB::insert($this->_table, $arr, true);
        }

        //更新数据库
        public function update($id, $data) {
            $id = intval($id);
            if (!$data) {
                return;
            }
            return DB::update($this->_table, $data, DB::field($this->_pk, $id));
        }

        //查找某条记录
        public function fetch_role_purview_info($id) {
            $id = intval($id);
            return DB::fetch_first("SELECT * FROM %t WHERE id=%d", array($this->_table, $id));
        }

        //查询所有limt数据
        public function role_purview_all_find($rid,$start = 0, $limit = 0) {
            return DB::fetch_all('SELECT rp.* , ar.name as rname ,ad.name as cadname FROM ' . DB::table($this->_table) . ' as rp inner join '.DB::table('cloud_app_role').' as ar on rp.rid=ar.id  '.
            ' inner join '.DB::table('cloud_app_district').' as ad on rp.cadid = ad.cadid '.
             ' where  rp.rid='.$rid.
            ' ORDER BY rp.sort asc ' . DB::limit($start, $limit));
        }


        //查询所有limt数据
        public function role_purview_all_not_lime($rid,$cadid=0) {
			if(!empty($cadid)){
				$cadid_where = ' and rp.cadid='.$cadid;
			}else{
				$cadid_where = ' ';
			}
            return DB::fetch_all('SELECT rp.* , ar.name as rname ,ad.name as cadname FROM ' . DB::table($this->_table) . ' as rp inner join '.DB::table('cloud_app_role').' as ar on rp.rid=ar.id  '.
            ' inner join '.DB::table('cloud_app_district').' as ad on rp.cadid = ad.cadid '.
            ' where  rp.rid='.$rid.$cadid_where.
            ' ORDER BY rp.sort asc ' );
        }


        //查询所有limt数据
        public function role_purview_all_sys($rid) {
            return DB::fetch_all('SELECT rp.* , ar.name as rname  FROM ' . DB::table($this->_table) . ' as rp inner join '.DB::table('cloud_app_role').' as ar on rp.rid=ar.id  '.
            ' where  rp.rid='.$rid.' and rp.cadid is null'.
            ' ORDER BY rp.sort asc ' );
        }

        //查询所有limt数据
        public function role_purview_all_count($rid) {
            return DB::fetch_first('SELECT count(id) as countnum FROM ' . DB::table($this->_table) . '  where rid='.$rid);
        }


        //查询所有limt数据
        public function role_purview_all_delete($rid) {
            return DB::query('DELETE FROM ' . DB::table($this->_table) . '  where rid='.$rid);
        }

        //查询所有limt数据
        public function role_purview_not_select_find($rid,$cadid,$caid) {
            return DB::fetch_all('select af.*, f.pid as fpid,f.name,f.linkurl,f.sort,f.icon,f.status from '.DB::table('cloud_app_function').' as af inner join '.DB::table('cloud_function').' as f on af.fid=f.id where caid ='.$caid.
            ' and fid not in (select fid from '.DB::table($this->_table).' where rid='.$rid.' and cadid='.$cadid.')');
        }


        //查询所有limt数据
        public function role_purviews_find($rid,$cadid,$id) {
            return DB::fetch_first('select * from '.DB::table($this->_table).'  where rid='.$rid.' and fid='.$id.' and cadid='.$cadid.'' );
        }


        //查询所有limt数据
        public function role_purview_delete_function($fpid,$cadid) {
            return DB::query('DELETE FROM ' . DB::table($this->_table) . '  where (fpid='.$fpid.' or fid= '.$fpid.') and cadid='.$cadid);
        }



    }